package com.wit.dao.impl;

import hibernate.HibernateSessionFactory;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;

import com.wit.dao.SubcompanyDao;
import com.wit.po.Subcompany;

public class SubcompanyDaoImpl implements SubcompanyDao {

	
	public boolean addSubcompany(Subcompany subcompany) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.save(subcompany);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public boolean deleteSubcompany(Subcompany subcompany) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.delete(subcompany);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public List<Subcompany> findSubcompanyByCondition(DetachedCriteria dc,Integer startRecord, Integer PageSize) {
		List<Subcompany> compList = new ArrayList<Subcompany>();
		try{
			compList = (List<Subcompany>)dc.getExecutableCriteria(HibernateSessionFactory.getSession()).add(Property.forName("company").isNotNull()).setFirstResult(startRecord).setMaxResults(PageSize).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return compList;
	}

	
	public Subcompany findSubcompanyByIdentifier(String subCompanyId) {
		Session s = null;
		Subcompany subcompany = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Subcompany> compList = (List<Subcompany>)s.createQuery("FROM Subcompany WHERE subcommpanyId = ?").setString(0, subCompanyId).list();
			if(compList.size() == 1)
				subcompany = compList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return subcompany;
	}

	
	public boolean modifySubcompany(Subcompany subcompany) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.update(subcompany);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public Integer findSubcompanyCount(DetachedCriteria dc) {
		Integer amount = null;
		try{
			amount = (Integer)dc.getExecutableCriteria(HibernateSessionFactory.getSession()).add(Property.forName("company").isNotNull()).uniqueResult();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return amount;
	}
	
	public static void main(String[] args){
		SubcompanyDaoImpl sdi = new SubcompanyDaoImpl();
		DetachedCriteria dc = DetachedCriteria.forClass(Subcompany.class);
		List<Subcompany> sy = sdi.findSubcompanyByCondition(dc, 0, 29);
		System.out.println(sy.size());
		
	}
}
